/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package lib.plan;

import lib.exceptions.InvalidLengthException;
import java.util.Calendar;
import java.util.List;
import java.util.ArrayList;
import lib.xml.XMLizable;

/**
 * This class represents an order with one or more items to do by the factory,
 * all the orders belong to a customer and they have a date when they were
 * recived and another order when they are supposed to be ready. The order has
 * an identifficative number to difference it from the other orders.
 * @see Item
 * @author Jorge Mira Yagüe
 */
public class Order implements XMLizable {

    private int number;
    private String customer;
    private Calendar dateReceived;
    private Calendar dateDue;
    private int priority;
    private List<Item> items;

    /**
     * Builds an empty order
     */
    public Order() {
        number = 0;
        customer = "";
        dateDue = null;
        dateReceived = null;
        priority = 0;
        items = new ArrayList<Item>();
    }

    /**
     * Builds an order with the parameters given
     * @param customer the customer of the order
     * @param dateRecived the date when the order is received
     * @param dateDue the date when the order it's supposed to be done
     * @param priority the priority of the order
     */
    public Order(String customer,
            Calendar dateRecived,
            Calendar dateDue,
            int priority)
            throws InvalidLengthException {
        if (!dateRecived.before(dateDue)) {
            throw new InvalidLengthException();
        }
        this.customer = customer;
        this.dateReceived = dateRecived;
        this.dateDue = dateDue;
        this.priority = priority;
        items = new ArrayList<Item>();
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public Calendar getDateDue() {
        return dateDue;
    }

    public Calendar getDateReceived() {
        return dateReceived;
    }

    public List<Item> getItems() {
        return items;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getPriority() {
        return priority;
    }

    /**
     * Adds an item to the order, setting his number to his position in the
     * order, if his priority is zero sets it to the order priority
     * @param item the item to be add to the order
     */
    public void addItem(Item item) {
        if (item.getPriority() == 0) {
            item.setPriority(this.priority);
        }
        item.setOrder(this.number);
        item.setNumber(items.size());
        items.add(item);
    }

    public Item getItem(int itemNumber) {
        return items.get(itemNumber);
    }

    /**
     * Removes an item from the order
     * @param itemNumber the item number of the item
     */
    public void removeItem(int itemNumber) {
        items.remove(itemNumber);
    }

    public String toXML() {
        String result = "<Order ";
        result += "number=\"" + number + "\" ";
        result += "customer=\"" + customer + "\" ";
        result += "dateReceived=\"" + dateReceived.getTimeInMillis() + "\" ";
        result += "dateDue=\"" + dateDue.getTimeInMillis() + "\" ";
        result += "priority=\"" + priority + "\" ";
        result += ">\n";
        if (items != null) {
            for (Item item : items) {
                result += item.toXML();
            }
        }
        result += "</Order>\n";
        return result;
    }
}
